package simbad.sim;

import javax.media.j3d.*;
import javax.vecmath.Color3f;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.geometry.Primitive;
/**
 * A box cylinder which can be put in the environment. 
 */
public class Cylinder extends BlockWorldObject {
    
    /** Object dimensions */
    float radius, height;
    
    /** Constructs a cylinder object.
     * 
     * @param pos position of the center of the object. 
     * @param extent - extent in the three directions.
     * @param wd	EnvironmentDescription for global parameters.
     */
    public Cylinder(Vector3d pos, float radius, float height, EnvironmentDescription wd) {        
        this(pos, radius, height,wd,wd.boxColor);
    }
    
    /** Constructs a cylinder object.
     * 
     * @param pos position of the center of the object. 
     * @param extent - extent in the three directions.
     * @param wd	EnvironmentDescription for global parameters.
     * @param color  color of the object.
     */
    public Cylinder(Vector3d pos, float radius, float height, EnvironmentDescription wd, Color3f color) {        
        this.radius = radius;
        this.height = height;
        // put it on the floor
        pos.y += height/2;
        create3D(wd,color);
        translateTo(pos);
    }
    /** Create the 3d object */
    protected void create3D(EnvironmentDescription wd,Color3f color){
        // create the box using a primitive
        super.create3D();
        Material mat = new Material();
        appearance.setMaterial(mat);
        
        int flags = Primitive.GEOMETRY_NOT_SHARED | Primitive.ENABLE_GEOMETRY_PICKING | Primitive.GENERATE_NORMALS;
        flags |= Primitive.ENABLE_APPEARANCE_MODIFY;
        com.sun.j3d.utils.geometry.Cylinder cyl = new com.sun.j3d.utils.geometry.Cylinder(radius, height, flags, appearance);
         // Enable  sensor  detection 
        cyl.setPickable(true);
	    
        //define the bounds for collision detection (use sphere as approximation)
        BoundingSphere bounds = new BoundingSphere();
	    bounds.setRadius(radius);
	    setBounds(bounds);
	    
	    setColor(color);
	    addChild(cyl);
    }

}

